﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CFSystem.JXC.IDAL;
using CFSystem.JXC.Model;
using System.Data;
using System.Data.OleDb;
using DBUtility;
using System.Globalization;

namespace CFSystem.JXC.AccessDAL
{
    public class StorageInfoSearch : IStorageInfo
    {
        #region IStorageInfo Members

        public IList<StorageInfo> GetAllStorageRecords()
        {
            string sqlSelect = "Select * from [tb_Inventory] a,[tb_GoodBase] b where a.[ProductID] =b.[ID]";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            //DataSet ds = AccessHelper.ExecuteDataset(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<StorageInfo> si = new List<StorageInfo>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    si.Add(GetStorageInfo(dr));
                }
                dr.Close();
            }
            return si;
        }

        private StorageInfo GetStorageInfo(OleDbDataReader dr)
        {
            StorageInfo si = new StorageInfo();
            si.ProductID = Convert.ToInt32(dr["ProductID"]);
            si.ProductName = dr["Name"].ToString();
            si.BatchIn = Convert.ToInt32(dr["BatchID"]);   //批次

            IFormatProvider culture = new CultureInfo("fr-FR", true);
            si.BuyDate = DateTime.Parse(dr["StockDate"].ToString(), culture); //采购日期
            si.CostSum = (Convert.ToDecimal(dr["StockPrice"])) * (Convert.ToInt32(dr["StockAmount"]));
            si.InvPrice = Convert.ToDecimal(dr["StockPrice"]);
            si.InvQty = Convert.ToInt32(dr["StockAmount"]);
            si.RefBuyPrice = Convert.ToDecimal(dr["RefSalePrice"]);
            #region obsolete
            //si.RefPrice = Convert.ToDecimal(dr["RefPrice"]);                // obsolete
            //si.SaleSum = (Convert.ToDecimal(dr["RefPrice"])) * (Convert.ToInt32(dr["InvCount"])); //obsolete
            //si.SizeID = Convert.ToString(dr["SizeID"]);  //obsolete.
            si.Operator = "Jackie";
            #endregion
            return si;
        }
        /// <summary>
        /// 查询库存记录
        /// </summary>
        /// <param name="strSearch"></param>
        /// <returns></returns>
        public IList<StorageInfo> SearchStorageRecords(string strSearch)
        {
            string sqlSelect = "Select * from [tb_Inventory] a,[tb_GoodBase] b where a.[ProductID] =b.[ID]" + strSearch;
            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<StorageInfo> si = new List<StorageInfo>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    si.Add(GetStorageInfo(dr));
                }
                dr.Close();
            }
            return si;
        }

        #endregion
    }


}
